package com.shaohai.live.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.shaohai.live.LiveVideoActivity;
import com.shaohai.live.R;
import com.shaohai.live.entity.Course;
import com.shaohai.live.utils.AppDefine;
import com.shaohai.live.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class CourseAdapter extends BaseAdapter implements Filterable {
    List<Course> dataArray;
    List<Course> filterArray;
    Context context;
    CourseAdapterFilter adapterFilter;

    public CourseAdapter(Context context, List<Course> courses) {
        this.dataArray = courses;
        this.filterArray = courses;
        this.context = context;
    }

    @Override
    public int getCount() {
        return filterArray.size();
    }

    @Override
    public Object getItem(int i) {
        return filterArray.get(i);
    }

    @Override
    public long getItemId(int i) {
        return filterArray.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ItemContainerView itemContainerView;
        if (convertView == null) {
            itemContainerView = new ItemContainerView();
            convertView = LayoutInflater.from(context).inflate(R.layout.course_waiting_list_item, null);
            itemContainerView.tv_title = convertView.findViewById(R.id.tv_title);
            itemContainerView.tv_summary = convertView.findViewById(R.id.tv_summary);
            itemContainerView.tv_time = convertView.findViewById(R.id.tv_time);
            itemContainerView.btnStart = convertView.findViewById(R.id.btnStart);
            convertView.setTag(itemContainerView);
        } else {
            itemContainerView = (ItemContainerView) convertView.getTag();
        }
        Course data = filterArray.get(position);
        itemContainerView.tv_title.setText(data.title);
        itemContainerView.tv_summary.setText(data.courseintroduction);
        itemContainerView.tv_time.setText(data.starttime.concat(" 至 ").concat(data.endtime));
        itemContainerView.btnStart.setOnClickListener(view -> {
            Intent intent = new Intent(context, LiveVideoActivity.class);
            intent.putExtra(AppDefine.TRANS_COURSE, data);
            context.startActivity(intent);
        });
        return convertView;
    }

    @Override
    public Filter getFilter() {
        if (this.adapterFilter == null) {
            this.adapterFilter = new CourseAdapterFilter();
        }
        return this.adapterFilter;
    }

    class CourseAdapterFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            FilterResults filterResults = new FilterResults();
            List<Course> filterList = new ArrayList<>();
            for (Course t : dataArray) {
                if (StringUtils.isNullOrEmpty(charSequence.toString())) {
                    filterList.add(t);
                }
            }
            filterResults.values = filterList;
            filterResults.count = filterList.size();
            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
            if (filterResults != null) {
                filterArray = (ArrayList<Course>) filterResults.values;
                if (filterArray == null) {
                    notifyDataSetInvalidated();
                    return;
                }
                Log.e("publishResults", "filter size is " + filterArray.size());
                if (filterArray.size() > 0)
                    notifyDataSetChanged();
                else
                    notifyDataSetInvalidated();
            } else {
                notifyDataSetInvalidated();
            }
        }
    }

    class ItemContainerView {
        TextView tv_title, tv_summary, tv_time;
        QMUIRoundButton btnStart;
    }
}
